//
//  OpenRedPacketVC.m
//  RPay
//
//  Created by hzy on 2017/11/2.
//  Copyright © 2017年 Charlie. All rights reserved.
//

#import "OpenRedPacketVC.h"
#import "RedPacketView.h"
#import "TMNetwork.h"
#import "RecordModel.h"
#import "RedPacketRecordCell.h"

@interface OpenRedPacketVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)RecordModel * model;
@property (nonatomic,strong)RedPacketView * redPacketView;


@end

@implementation OpenRedPacketVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"红包";
    [self openRednetwork];
    [self addNavigationBarLeftButtonItem];

}
-(void)setupUI{
    _redPacketView = [[[NSBundle mainBundle] loadNibNamed:@"RedPacketView" owner:nil options:nil]lastObject];
    _redPacketView.frame = CGRectMake(0, 0, DEVICE_WITH, CGRectGetHeight(self.view.frame));
    [self.view addSubview:_redPacketView];
    _redPacketView.fromNameLab.text = [NSString stringWithFormat:@"%@的红包",_model.sendUser];
    _redPacketView.msgLab.text = _model.msg;
    _redPacketView.tableView.delegate = self;
    _redPacketView.tableView.dataSource = self;
    NSString * totleString = [NSString stringWithFormat:@"%ld个红包",_model.packetCount];
    NSString * string = nil;
    if (_model.takeRecord.count == _model.takeUserArray.count) {
        string = @",已被抢光";

    }else{
    
        string = [NSString stringWithFormat:@"，已领取%ld个。",_model.takeRecord.count];
    }
    _redPacketView.numLab.text = [NSString stringWithFormat:@"%@%@",totleString,string];
    
    [self.view addSubview:_redPacketView];

}

-(void)openRednetwork{
    
    
    if (self.aid) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [[TMNetwork sharedInstance] GET:@"/open/appRedPacket/getPacketById.shtml?" headerParams:nil queryParams:nil formParams:@{@"id":self.aid,@"type":_isMine?@"send":@"get"} withToken:NO model:nil progress:^(NSProgress *progress) {
            
        } success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            
            if ([self checkNetworkData:responseObject]) {
                
                
                _model = [RecordModel mj_objectWithKeyValues:responseObject[@"content"]];
                [self setupUI];
                
            }
            
        } failure:^(id responseObject) {
            [self alertNetworkError];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  _model.takeRecord.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static  NSString  *CellIdentiferId = @"RedPacketRecordCell";
    RedPacketRecordCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"RedPacketRecordCell" owner:nil options:nil];
        cell = [nibs lastObject];
        cell.backgroundColor = [UIColor clearColor];
    };

    cell.fromLab.text = [NSString stringWithFormat:@"%@",_model.takeRecord[indexPath.row][@"takeInUser"]];
    cell.moneyLab.text = [NSString stringWithFormat:@"%.2f",[_model.takeRecord[indexPath.row][@"takeInMoney"] doubleValue]];
    NSDate * date = [RecordModel dateWithInt64_t:[_model.takeRecord[indexPath.row][@"takeInTime"] longLongValue]/1000];
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    cell.timeLab.text = [formatter stringFromDate:date];
    
    

    
    return cell;
}
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return NO;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [_redPacketView.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
    
    
}




@end
